package com.hottv.meeting.videomeeting.module.meeting;

import android.content.Context;
import android.view.SurfaceView;

import com.google.gson.Gson;
import com.hottv.meeting.videomeeting.common.MeetingConfig;
import com.hottv.meeting.videomeeting.common.MeetingUtils;
import com.hottv.meeting.videomeeting.module.edp.EDPUtils;
import com.hottv.meeting.videomeeting.module.meeting.bean.HotMessage;
import com.hottv.meeting.videomeeting.module.meeting.data.MeetInfo;
import com.hottv.meeting.videomeeting.module.meeting.data.MemItemInfo;
import com.hottv.meeting.videomeeting.module.meeting.view.VideoData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.OkHttpClient;

/**
 * Created by chengwenqiang on 2019/6/5.
 */

public class MeetingManager {

    private List<MemItemInfo> mList =new ArrayList<>();
    private MeetingManager()
    {
        mList.add(new MemItemInfo("苏       总","13802882313",530396130));
        mList.add(new MemItemInfo("张  潮  州","13760289294",528981534));
        mList.add(new MemItemInfo("程  文  强","13802885322",528876838));
        mList.add(new MemItemInfo("钟  伟  开","15918611748",528998189));
        mList.add(new MemItemInfo("大学城电视","02061888888",529208676));
        mList.add(new MemItemInfo("琶洲电视","02061999999",530304905));
        mList.add(new MemItemInfo("程  文  强new","13802885322",9969401784l));
        mList.add(new MemItemInfo("苏总new","13802885322",9923970418l));
        mList.add(new MemItemInfo("潮州new","13802885322",9923970418l));
    }

    private Map<Integer,SurfaceView> mSurfaceList = new HashMap<>();

    private static MeetingManager instance;

    private static MeetInfo meetInfo;

    private int mRemoteUid =0;

    //参加会议人数
    private List<HotMessage.UserBean> mVideoList =new ArrayList<>();

    public static  MeetingManager getInstance()
    {
        if(instance==null)
        {
            instance = new MeetingManager();
        }
        return   instance;
    }

    public List<MemItemInfo> getMemList()
    {
        return mList;
    }

    public String getNameFromDeviceId(long deviceId)
    {
        for(int i=0;i<mList.size();i++)
        {
            if(mList.get(i).deviceId==deviceId)
            {
                return mList.get(i).name;
            }
        }

        return "";
    }

    public String getNameFromDeviceId(String deviceId)
    {
        for(int i=0;i<mList.size();i++)
        {
            if(String.valueOf(mList.get(i).deviceId).equals(deviceId))
            {
                return mList.get(i).name;
            }
        }

        return "";
    }

    public long getDeviceIdFromPhone(String phone)
    {
        for(int i=0;i<mList.size();i++)
        {
            if(mList.get(i).phone.equals(phone))
            {
                return mList.get(i).deviceId;
            }
        }

        return 0;
    }

    public String getNameFromPhone(String phone)
    {
        for(int i=0;i<mList.size();i++)
        {
            if(mList.get(i).phone.equals(phone))
            {
                return mList.get(i).name;
            }
        }

        return "";
    }

    public void AddMem(MeetInfo info)
    {
        meetInfo = info;
    }

    public MeetInfo getMeetInfo()
    {
        return  meetInfo;
    }

    public void addUserBean(HotMessage.UserBean userBean)
    {
        boolean isFlag = false;
        for(int i=0;i<mVideoList.size();i++)
        {
            if(userBean.getUserId()==mVideoList.get(i).getUserId())
            {
                isFlag =true;
                break;
            }
        }
        if(!isFlag)
        {
            mVideoList.add(userBean);
        }
    }

    public void removeUserBean(HotMessage.UserBean userBean)
    {
        boolean isFlag = false;
        for(int i=0;i<mVideoList.size();i++)
        {
            if(userBean.getUserId()==mVideoList.get(i).getUserId())
            {
                mVideoList.remove(i);
                break;
            }
        }
    }

    public void removeUserBean(int deviceId)
    {
        boolean isFlag = false;
        for(int i=0;i<mVideoList.size();i++)
        {
            if(deviceId==mVideoList.get(i).getUserId())
            {
                mVideoList.remove(i);
                break;
            }
        }
    }

    public HotMessage.UserBean getUserBean(int deviceId)
    {
        boolean isFlag = false;
        for(int i=0;i<mVideoList.size();i++)
        {
            if(deviceId==mVideoList.get(i).getUserId())
            {
                return   mVideoList.get(i);
            }
        }
        return  null;
    }

    //获取参数人数
    public int getUserNum()
    {
        return mVideoList.size();
    }

    public SurfaceView getSurfaceViewById(int uid)
    {
        if(mSurfaceList.containsKey(uid))
            return mSurfaceList.get(uid);
        return  null;
    }

    public void insertSurfaceView(int uid,SurfaceView surface)
    {
        if(!mSurfaceList.containsKey(uid))
        {
            mSurfaceList.put(uid,surface);
        }
    }

    public void removeSurfaceView(int uid)
    {
        if(mSurfaceList.containsKey(uid))
        {
            mSurfaceList.remove(uid);
        }
    }

    public void destroy()
    {
        mRemoteUid = 0;
        mSurfaceList.clear();
        mVideoList.clear();
        mMeetingBean =new HotMessage.MeetingBean();
        mBroadcastBean = new HotMessage.BroadcastBean();
    }

    public int getSurfaceViewNum()
    {
        return mSurfaceList.size();
    }

    public void setRemoteUid(int uid)
    {
        mRemoteUid = uid;
    }

    public int getRemoteUid()
    {
        return mRemoteUid;
    }

    public List<String> getSurfaceKeys()
    {
        List<String> mList =new ArrayList<>();
        Set<Integer> keySet = mSurfaceList.keySet();
        Iterator<Integer> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key =String.valueOf(iter.next()) ;
            mList.add(key);
        }

        return mList;
    }

    public String getDeviceIdFromPostion(int postion)
    {
        Set<Integer> keySet = mSurfaceList.keySet();
        Iterator<Integer> iter = keySet.iterator();
        int index = 0;
        while (iter.hasNext()) {
            if(postion==index)
            {
                return  String.valueOf(iter.next()) ;
            }
            index++;
        }
        return  "";
    }

    private HotMessage.MeetingBean mMeetingBean =new HotMessage.MeetingBean();
    //设置开会人员信息
    public void setMeetingBean(HotMessage.MeetingBean bean)
    {
        mMeetingBean = bean;
    }

    private HotMessage.BroadcastBean mBroadcastBean = new HotMessage.BroadcastBean();
    //设置当前主播信息
    public void setBroadcastBean(HotMessage.BroadcastBean bean)
    {
        mBroadcastBean =bean;
    }


    //获取会议创始人信息
    public HotMessage.MeetingBean getMeetingBean()
    {
        return mMeetingBean;
    }

    //获取会议主播
    public HotMessage.BroadcastBean getBroadcastBean()
    {
        return mBroadcastBean;
    }


}
